#!/usr/bin/python
#coding=utf-8
#fileName:contact.py

import os,cPickle as p

class Contact:
    def __init__(self,data):
        if os.path.isfile(data):
            pass
        else:
            f = file(data ,'w')
            f.close()
        self.data = data;
    
    def add(self,name,phone):
        '''Add to contact.
        
        add a name and phone to contact.'''
        f = file(self.data)
        if os.path.getsize(self.data) > 0 :
            contact = p.load(f)
        else:
            contact = {}
        contact[name] = phone
            
        
        contactFile = file(self.data,'w')
        p.dump(contact,contactFile)
        contactFile.close();

    def delContact(self,name):
        '''Delete a contact.
        
        delete a name of contact.'''
        f = file(self.data)
        contact = p.load(f)
        if contact.has_key(name):
            del contact[name]
            contactFile = file(self.data,'w')
            p.dump(contact, contactFile)
            contactFile.close()
        else: 
            pass
        
    def getPhone(self,name=''):
        if name=='' :
            return self.listAll()
        else :
           f = file(self.data)
           contact = p.load(f)
           f.close()
           return contact[name]
            
    def listAll(self):
        f = file(self.data)
        if os.path.getsize(self.data) > 0 :
            contact = p.load(f)
        else:
            contact = {}
        return contact

    def change(self,name,phone):
        self.add(name, phone)
        
    def __del__(self):
        print 'Finish contact,Bye! Version v1.0.0 %s' %self.data
    
    




    